<?php

namespace app\rental\controller;

use cmf\controller\HomeBaseController;
use app\rental\model\RentalCategoryModel;
use app\rental\model\RentalCarModel;
use app\rental\model\RentalReserveModel;

use think\Db;

class CarController extends HomeBaseController
{
    public function _initialize()
    {
        parent::_initialize();
        define('MODULE_NAME', request()->module());   //模块名称
        define('CONTROLLER_NAME', request()->controller());   //控制器名称
        define('ACTION_NAME', request()->action());
    }
    public function index()
    {
        $rentalCategoryModel = new RentalCategoryModel();
        $rentalCarModel = new RentalCarModel();
        // $postService         = new PostService();
        $this->assign("categorys", $rentalCategoryModel->getIndexCategorys());
        $this->assign("rental_category", $rentalCategoryModel->getRentalCategory());
        $carId  = $this->request->param('id', 0, 'intval');
        $categoryId = $this->request->param('cid', 0, 'intval');

        if (empty($carId)) {
            abort(404, '汽车不存在!');
        }

        $car = $rentalCarModel->where('id',$carId)->find();
        $rentalCarModel->where(['id' => $carId])->setInc('post_hits');
       // dump(($car['categories'][0]));
        // $prevCar = $postService->publishedPrevCar($carId, $categoryId);
        // $nextCar = $postService->publishedNextCar($carId, $categoryId);

        $tplName = 'carinfo';
        if (empty($categoryId)) {
            $categories = $car['categories'];
            if (count($categories) > 0) {
                $this->assign('category', $categories);
            } else {
                abort(404, '汽车未指定分类!');
            }
            $tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];
        } else {
            $category = $rentalCategoryModel->where('id', $categoryId)->where('status', 1)->find();
            if (empty($category)) {
                abort(404, '汽车不存在!');
            }

            $this->assign('category', $category);

            $tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];
        }
        $this->assign('car', $car);
        return $this->fetch("/$tplName");
    }
    public function reserve(){
        if(request()->isPost()){
            $data   = $this->request->param();
            $result = $this->validate($data, 'RentalReserve');
            if ($result !== true) {
                $this->error($result);
            }
            $rentalReserveModel = new RentalReserveModel();
            $result = $rentalReserveModel->isUpdate(false)->allowField(true)->save($data);
            if($result!=false){
                $this->success('提交成功，请等待客服与您联系!');
            }
            //$this->error('您输入的电话号码有误！');
        }else{
            $rentalCarModel = new RentalCarModel();
            $rentalCategoryModel = new RentalCategoryModel();
            $carId  = $this->request->param('id', 0, 'intval');
            $categoryId = $this->request->param('cid', 0, 'intval');
            $category = $rentalCategoryModel->where('id', $categoryId)->where('status', 1)->find();
            $this->assign('category', $category);
            $car = $rentalCarModel->where('id',$carId)->find();
            $this->assign('car', $car);
            return $this->fetch('/rentallist');
        }
    }
    public function refreshAjax(){
        $rentalCarModel = new RentalCarModel();
        $rentalCategoryModel = new RentalCategoryModel();
        $rental_category = $rentalCategoryModel->getRentalCategory();
        $refreshCars = $rentalCarModel->order("RAND()")->limit('4')->select();
        $html ="";
        if(empty($refreshCars)){
            return '<div class="text-center">没有更多了</div>';
        }

        foreach($refreshCars as $key=>$val){
            $url = cmf_url( 'rental/Car/index',array( 'id'=>$val['id'],'cid'=>$val['category_id']));
            $src = cmf_get_image_url($val['more']['thumbnail']);
            $title = $val['title'];
        $html .= <<<HTML
            <div class="col-md-3 car-box">
                <div class="tc-gridbox">
                    <a href="{$url}">
                        <div class="header">
                            <div class="item-image">
                                <img src="{$src}" class="img-responsive" alt="">
                            </div>
                            <h3 class="title">
                                {$title}
                            </h3>
                            <div class="title price">
                                <span class="now-price">一口价：{$val['primary']['price']}万</span>&nbsp;&nbsp;&nbsp;<span class="m-price">市场价：{$val['primary']['m_price']}万</span>
                            </div>
                        </div>
                    </a>
                </div>
            </div>
HTML;
        }
        return $html;
    }

}
